package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.provider.dto.DeploymentDto;
import com.csun.cmny.provider.model.domain.OmcMerchandise;
import com.csun.cmny.provider.model.domain.OmcServiceFirstLevel;
import com.csun.cmny.provider.model.domain.OmcServiceSecondLevel;
import com.csun.cmny.provider.model.dto.service.ServiceSecondLevelSaveDto;
import com.csun.cmny.provider.model.dto.service.ServiceSecondLevelSelectDto;
import com.csun.cmny.provider.model.dto.serviceSecongLevel.SecondLevelIdsDto;
import com.csun.cmny.provider.model.vo.service.ServiceSecondLevelVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcMerchandiseService;
import com.csun.cmny.provider.service.OmcServiceFirstLevelService;
import com.csun.cmny.provider.service.OmcServiceSecondLevelService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/serviceLevel", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcServiceSecondLevelController {

    @Resource
    private OmcServiceSecondLevelService serviceSecondLevelService;

    @Resource
    private OmcServiceFirstLevelService firstLevelService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcMerchandiseService  merchandiseService;


    /*新增二级服务类别*/
    @ValidateAnnotation
    @RequestMapping(value = "/addServiceLevel", method = RequestMethod.POST)
    public Wrapper addServiceLevel( @RequestBody @Valid ServiceSecondLevelSaveDto serviceSecondLevelSaveDto,BindingResult br) {

        //校验此服务类别id是否存在
        OmcServiceFirstLevel omcServiceFirstLevel = firstLevelService.selectByKey(serviceSecondLevelSaveDto.getFirstLevelId());
        if (PublicUtil.isEmpty(omcServiceFirstLevel)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("service.firstLevelId.error", null));
        }

        //校验这个二级服务类别是否存在
        OmcServiceSecondLevel level = new OmcServiceSecondLevel();
        level.setSecondLevelName(serviceSecondLevelSaveDto.getSecondLevelName());
        List<OmcServiceSecondLevel> select = serviceSecondLevelService.select(level);
        if (PublicUtil.isNotEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("service.secondLevelName.error", null));
        }

        OmcServiceSecondLevel omcServiceSecondLevel = new OmcServiceSecondLevel();
        BeanUtils.copyProperties(serviceSecondLevelSaveDto,omcServiceSecondLevel);
        int result = serviceSecondLevelService.save(omcServiceSecondLevel);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /*查询二级服务类别*/
    @ValidateAnnotation
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Wrapper select( @RequestBody ServiceSecondLevelSelectDto serviceSecondLevelSelectDto) {

        OmcServiceSecondLevel omcServiceSecondLevel = new OmcServiceSecondLevel();
        omcServiceSecondLevel.setFirstLevelId(serviceSecondLevelSelectDto.getFirstLevelId());
        List<OmcServiceSecondLevel> list = serviceSecondLevelService.select(omcServiceSecondLevel);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),list);
    }

    /*删除二级服务类别*/
    @ValidateAnnotation
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Wrapper delete(Long id) {

        OmcMerchandise merchandise = new OmcMerchandise();
        merchandise.setSecondLevellId(id);

        List<OmcMerchandise> select = merchandiseService.select(merchandise);

        if (PublicUtil.isNotEmpty(select)){
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("service.second.level.delete.error", null));
        }

        int result = serviceSecondLevelService.deleteByKey(id);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }


}
